package com.tloss.stdao.service;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import com.tloss.stdao.JPAResourceBean;
import com.tloss.stdao.User;

public class UserService implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4653850309893217863L;

	public List<User> select() {
		EntityManager em = JPAResourceBean.getEMF().createEntityManager();
		Query q = em.createQuery("select t from User t");
		List<User> listUsers = q.getResultList();
		em.close();
		return listUsers;
	}

	public User select(String username) {
		EntityManager em = JPAResourceBean.getEMF().createEntityManager();
		User user = em.find(User.class, username);
		em.clear();
		return user;
	}

	public void insert(User user) {
		EntityManager em = JPAResourceBean.getEMF().createEntityManager();
		EntityTransaction transaction = em.getTransaction();
		transaction.begin();
		em.persist(user);
		transaction.commit();
		em.close();
	}
}
